# Maintainer: Christopher Degawa <ccom@randomderp.com>

_realname=ripgrep
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=13.0.0
pkgrel=1
pkgdesc='line-oriented search tool that recursively searches your current directory for a regex pattern (mingw-w64)'
arch=(any)
mingw_arch=(mingw32 mingw64 ucrt64 clang64 clangarm64)
url=https://github.com/BurntSushi/$_realname
license=(MIT custom:UNLICENSE)
depends=("${MINGW_PACKAGE_PREFIX}-pcre2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-jq"
             $([[ ${MSYSTEM_CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-asciidoctor")
             git)
source=("git+$url#tag=13.0.0")
sha512sums=('SKIP')

prepare() {
  cd "${srcdir}/${_realname}"

  cargo fetch --locked
}

build() {
    cd "${srcdir}/${_realname}" || return 1

    cargo build --release --frozen --features 'pcre2' --message-format=json-render-diagnostics | tee /dev/shm/a |
    jq -r 'select(.out_dir) | select(.package_id | startswith("ripgrep ")) | .out_dir' > out_dir
}

package() {
  cd "${srcdir}/${_realname}" || return 1

  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"
  local OUT_DIR=$(<out_dir)

  install -Dm644 "complete/_rg" "$pkgdir/$MINGW_PREFIX/share/zsh/site-functions/_rg"
  install -Dm644 "$OUT_DIR"/rg.bash "$pkgdir/$MINGW_PREFIX/share/bash-completion/completions/rg"
  install -Dm644 "$OUT_DIR"/rg.fish "$pkgdir/$MINGW_PREFIX/share/fish/vendor_completions.d/rg.fish"
  install -Dm644 COPYING LICENSE-MIT UNLICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
